package com.offcn.user.component;

import com.offcn.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
@Slf4j
@Component
public class SmsTemplate {

    //短信发送请求网关地址
    @Value("${sms.host}")
    public String host;

    @Value("${sms.path}")
    public String path;

    @Value("${sms.method:POST}")
    public String method;

    @Value("${sms.appcode}")
    public String appcode;


    //短信发送调用方法
    public String smsSend(Map<String,String> querys){
        try {
            //创建请求头集合
            Map<String,String> heads=new HashMap<>();
            heads.put("Authorization","APPCODE "+appcode);

            //请求体集合
            Map<String,String> bodys=new HashMap<>();

            //调用httpUtils工具类，发出短信发送请求
            HttpResponse response = HttpUtils.doPost(host, path, method, heads, querys, bodys);

          String responseStr=EntityUtils.toString(response.getEntity(),"utf-8");
           log.info("短信发送完成,响应数据:{}",responseStr);
           return responseStr;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("短信发送失败,请求参数为:{}",querys);
            return "fail";
        }



    }
}
